//
//  RCBaseConversion.m
//  StructToolsDemo
//
//  Created by 杨晓辉 on 2017/9/18.
//  Copyright © 2017年 杨晓辉. All rights reserved.
//

#import "RCBaseConversion.h"

@implementation RCBaseConversion
/**
 十进制转换为二进制
 
 @param decimal 十进制
 @return 二进制
 */
+ (NSString*)getBinaryByDecimal:(NSInteger)decimal
{
    NSString *binary = @"";
    while (decimal) {
        
        binary = [[NSString stringWithFormat:@"%ld", decimal % 2] stringByAppendingString:binary];
        if (decimal / 2 < 1) {
            
            break;
        }
        decimal = decimal / 2 ;
    }
    if (binary.length % 4 != 0) {
        
        NSMutableString *mStr = [[NSMutableString alloc]init];;
        for (int i = 0; i < 4 - binary.length % 4; i++) {
            
            [mStr appendString:@"0"];
        }
        binary = [mStr stringByAppendingString:binary];
    }
    return binary;
    
}

/**
 十进制转换成十六进制
 
 @param decimal 十进制
 @return 十六进制
 */
+ (NSString*)getHexByDecimal:(NSInteger)decimal
{
    NSString *hex =@"";
    NSString *letter;
    NSInteger number;
    for (int i = 0; i<9; i++) {
        
        number = decimal % 16;
        decimal = decimal / 16;
        switch (number) {
                
            case 10:
                letter =@"A"; break;
            case 11:
                letter =@"B"; break;
            case 12:
                letter =@"C"; break;
            case 13:
                letter =@"D"; break;
            case 14:
                letter =@"E"; break;
            case 15:
                letter =@"F"; break;
            default:
                letter = [NSString stringWithFormat:@"%ld", (long)number];
        }
        hex = [letter stringByAppendingString:hex];
        if (decimal == 0) {
            
            break;
        }
    }
    return hex;
}


/**
 十进制转八进制
 
 @param decimal 十进制
 @return 八进制
 */
+ (NSString*)getOctalByDecimal:(NSInteger)decimal
{
    NSMutableString *mstring = [NSMutableString stringWithFormat:@"X"];
    NSString *bitString = [NSString stringWithFormat:@"0123456789ABCDEF"];
    
    
    NSInteger a = 7;
    NSInteger b = 3;
    
    while(decimal!=0)
    {
        NSInteger p=decimal&a;
        NSString *str1=[NSString stringWithFormat:@"%c",[bitString characterAtIndex:p]];
        [mstring insertString:str1 atIndex:0];
        decimal=decimal>>b;
    }
    
    return [mstring substringWithRange:NSMakeRange(0, [mstring length]-1)];
    
}
/**
 十六进制转换成二进制
 
 @param hex 十六进制
 @return 二进制
 */
+ (NSString*)getBinaryByHex:(NSString*)hex
{
    NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [hexDic setObject:@"0000" forKey:@"0"];
    [hexDic setObject:@"0001" forKey:@"1"];
    [hexDic setObject:@"0010" forKey:@"2"];
    [hexDic setObject:@"0011" forKey:@"3"];
    [hexDic setObject:@"0100" forKey:@"4"];
    [hexDic setObject:@"0101" forKey:@"5"];
    [hexDic setObject:@"0110" forKey:@"6"];
    [hexDic setObject:@"0111" forKey:@"7"];
    [hexDic setObject:@"1000" forKey:@"8"];
    [hexDic setObject:@"1001" forKey:@"9"];
    [hexDic setObject:@"1010" forKey:@"A"];
    [hexDic setObject:@"1011" forKey:@"B"];
    [hexDic setObject:@"1100" forKey:@"C"];
    [hexDic setObject:@"1101" forKey:@"D"];
    [hexDic setObject:@"1110" forKey:@"E"];
    [hexDic setObject:@"1111" forKey:@"F"];
    
    NSString *binary = @"";
    for (int i=0; i<[hex length]; i++) {
        
        NSString *key = [hex substringWithRange:NSMakeRange(i, 1)];
        NSString *value = [hexDic objectForKey:key.uppercaseString];
        if (value) {
            
            binary = [binary stringByAppendingString:value];
        }
    }
    return binary;
    
}

/**
 十六进制转换成十进制
 
 @param hex 十六进制
 @return 十进制
 */
+ (NSInteger)getDecimalByHex:(NSString*)hex
{
    //    十六进制先转成二进制
    NSString *binary = [self getBinaryByHex:hex];
    //    二进制转为十进制
    NSInteger decimal = [self getDecimalByBinary:binary];
    return decimal;
}

/**
 十六进制转换成八进制
 
 @param hex 十六进制
 @return 八进制
 */
+ (NSString*)getOctalByHex:(NSString*)hex
{
    NSInteger decimal = [self getDecimalByHex:hex];
    NSString *octal = [self getOctalByDecimal:decimal];
    return octal;
}

/**
 二进制转换成十六进制
 
 @param binary 二进制
 @return 十六进制
 */
+ (NSString*)getHexByBinary:(NSString*)binary
{
    //    二进制转换成十进制
    NSInteger decimal = [self getDecimalByBinary:binary];
    //    十进制转换十六进制
    NSString *hex = [self getHexByDecimal:decimal];
    
    return hex;
}
/**
 二进制转换成十进制
 
 @param binary 二进制
 @return 十进制
 */
+ (NSInteger)getDecimalByBinary:(NSString*)binary
{
    
    NSInteger decimal = 0;
    for (int i=0; i<binary.length; i++) {
        
        NSString *number = [binary substringWithRange:NSMakeRange(binary.length - i - 1, 1)];
        if ([number isEqualToString:@"1"]) {
            
            decimal += pow(2, i);
        }
    }
    return decimal;
    
}


/**
 二进制转换成八进制
 
 @param binary 二进制
 @return 八进制
 */
+ (NSString*)getOctalByBinary:(NSString*)binary
{
    NSInteger decimal = [self getDecimalByBinary:binary];
    NSString *octal = [self getOctalByDecimal:decimal];
    
    return octal;
}
/**
 八进制转换成二进制
 
 @param octal 八进制
 @return 二进制
 */
+ (NSString*)getBinaryByOctal:(NSString*)octal
{
    NSInteger decimal = [self getDecimalByOctal:octal];
    NSString *binary = [self getBinaryByDecimal:decimal];
    return binary;
}

// 八进制转换成十进制
+ (NSInteger)getDecimalByOctal:(NSString*)octal
{
    long num = 0;
    NSMutableArray * arr = [NSMutableArray arrayWithCapacity:0];
    // 遍历字符串，按字符来遍历。每个字符将通过block参数中的substring传出
    [octal enumerateSubstringsInRange:NSMakeRange(0, octal.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        
        [arr addObject:substring];
    }];
    for (int i=0; i<arr.count; i++) {
        num+= [arr[i] integerValue] * pow(8, arr.count-1-i);
    }
    return num;
}

/**
 八进制转换成十六进制
 
 @param octal 八进制
 @return 十六进制
 */
+ (NSString*)getHexByOctal:(NSString*)octal
{
    NSInteger decimal = [self getDecimalByOctal:octal];
    NSString *hex = [self getHexByDecimal:decimal];
    return hex;
}
@end
